package org.Shiikong_Memory.SakuraBlockFrame.CommandFramework;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;

public class ArgumentConverter {
    public static Object convert(String value, Class<?> targetType) throws NumberFormatException {
        if (value == null || value.isEmpty()){
            return getDefaultValue(targetType);
        }else{
            if (targetType == String.class) return value;
            else if (targetType == Integer.class || targetType == int.class) return Integer.parseInt(value);
            else if (targetType == Double.class || targetType == double.class) return Double.parseDouble(value);
            else if (targetType == Boolean.class || targetType == boolean.class) return Boolean.parseBoolean(value);
            else if (targetType == Player.class) return Bukkit.getPlayerExact(value);
            else if (targetType == World.class) return Bukkit.getWorld(value);
        }
        return null;
    }

    public static Object getDefaultValue(Class<?> targetType) {
        if (targetType == String.class) return null;
        else if (targetType == Integer.class  || targetType == int.class) return 0;
        else if (targetType == Double.class  || targetType == double.class) return 0;
        else if (targetType == Boolean.class  || targetType == boolean.class) return null;
        else if (targetType == Player.class) return null;
        else if (targetType == World.class) return null;
        return null;
    }

    public static ArgType convertArgType(Class<?> targetType) {
        if (targetType == String.class) return ArgType.STRING;
        else if (targetType == Integer.class || targetType == int.class) return ArgType.INTEGER;
        else if (targetType == Double.class || targetType == double.class) return ArgType.DOUBLE;
        else if (targetType == Boolean.class || targetType == boolean.class) return ArgType.BOOLEAN;
        else if (targetType == Player.class) return ArgType.PLAYER;
        else if (targetType == World.class) return ArgType.WORLD;
        return ArgType.STRING;
    }
}
